import { createRouter, createWebHistory } from "vue-router";
import { showToast } from "vant";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    //首页
    {
      path: "/",
      redirect: "/home",
    },
    {
      path: "/home",
      name: "home",
      meta:{
        navshow:true,
      },
      component: () => import("../views/home.vue"),
    },
    //搜索
    {
      path: "/search",
      name: "search",
      component: () => import("../views/home/search.vue"),
    },
    //购物车
    {
      path: "/goods",
      name: "goods",
      //防跳
      beforeEnter(to, from, next) {
        if (localStorage.getItem("token")) {
          next();
        } else {
          next("/login");
        }
      },
      component: () => import("../views/goods.vue"),
    },
    //登录页
    {
      path: "/login",
      name: "login",
      //防止重复登录
      //路由独享守卫
      beforeEnter(to, from, next) {
        if (localStorage.getItem("token")) {
          // next();
          showToast({
                  message: "已登录",
                  icon: "success",
                })
                next("/home")
        } else {
          next()
        }
      },
      component: () => import("../views/login.vue"),
    },
    //我的
    {
      path: "/mine",
      name: "mine",
      beforeEnter(to, from, next) {
        if (localStorage.getItem("token")) {
          next();
        } else {
          next("/login");
        }
      },
      component: () => import("../views/my/mine.vue"),
    },
    //详情页,动态路由
    {
      path: "/details/:proid",
      name: "details",
      component: () => import("../views/goods/details.vue"),
    },
//添加地址
    {
      path: "/address",
      name: "address",
      component: () => import("../views/my/address.vue"),
    },
    //订单
    {
      path: "/order",
      name: "order",
      component: () => import("../views/my/order.vue"),
    },
    //注册
    {
      path: "/regist",
      name: "regist",
      component: () => import("../views/regist/regist.vue"),
      children: [
        {
          path: "step1",
          name: "step1",
          component: () => import("../views/regist/step1.vue"),
        },
        {
          path: "step2",
          name: "step2",
          component: () => import("../views/regist/step2.vue"),
        },
        {
          path: "step3",
          name: "step3",
          component: () => import("../views/regist/step3.vue"),
        },
      ],
    },
  ],
});

export default router;
